/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.hagopa.games.moneypool.msgs.ContentTypes.service;

/**
 *
 * @author Igor
 */
import java.nio.ByteBuffer;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import org.hagopa.games.moneypool.msgs.IMsgContent;

public class JoinResponse implements IMsgContent{
    private int tsize = 0;
    private int elems_no = 0;
    private List<JoinRespRecord> joinrecords = new ArrayList<JoinRespRecord>();
    
    
    public int getSize() {
	return tsize + 4;
    }

    public void printallrecords()
    {
        Iterator <JoinRespRecord> itr = joinrecords.iterator();
        System.out.println("Total elements:" + elems_no + " (elements in list:" + joinrecords.size() + " )");
        System.out.println("--------------------------------------------------------------------");
            while(itr.hasNext())
            {
                JoinRespRecord tmp = (JoinRespRecord)itr.next();
                System.out.println("Record:\n");
                System.out.println("Client Id: " + tmp.getcid());
                System.out.println("Player Name: " + tmp.getname());
                System.out.println("Size: " + tmp.getsz());
                System.out.println("###---###---###");
            }
        System.out.println("--------------------------------------------------------------------");

    }
    public int getNumOfElements()
    {
        return elems_no;
    }



   
    public void pushNewRecord(int cid, String name)
    {
        JoinRespRecord tmp = new JoinRespRecord();
        tmp.setcid(cid);
        tmp.setname(name);
        this.joinrecords.add(tmp);
        elems_no++;
        System.out.println("[JoinResponse](pushNewRecord)Total elements:" + elems_no);
        this.tsize += tmp.getsz();
        System.out.println("Total size now:" + this.tsize);
    }

    
    public byte[] marshallToByte()
    {
            byte [] bytes = new byte[1024];
           
            System.arraycopy(JoinResponse.intToByteArray(elems_no), 0, bytes, 0, 4);
            Iterator <JoinRespRecord> itr = joinrecords.iterator();
            int offset = 4;
            while(itr.hasNext())
            {

                JoinRespRecord tmp = (JoinRespRecord)itr.next();
                System.arraycopy(tmp.marshallToByte(), 0, bytes, offset, tmp.getsz());
                offset += tmp.getsz();
                //dataArray.put(tmp.marshallToByte());
                System.out.println("[JoinResponse](marshallToByte) - Added another Record. size - " + tmp.getsz());
            }

            return bytes;
    }
    
    public static byte[] intToByteArray(int value) {
        byte[] b = new byte[4];
        for (int i = 0; i < 4; i++) {
            int offset = (b.length - 1 - i) * 8;
            b[i] = (byte) ((value >>> offset) & 0xFF);
        }
        return b;
    }
    public boolean unmarshallFromByte(byte[] content)
    {
        //TODO - finish it once:)
	ByteBuffer dataArray = ByteBuffer.wrap(content);
	if(null == dataArray)
	{
        	return false;
	}
	dataArray.clear();
	return true;
    }
    public static final int byteArrayToInt(byte [] b) {
        return (b[0] << 24)
                + ((b[1] & 0xFF) << 16)
                + ((b[2] & 0xFF) << 8)
                + (b[3] & 0xFF);
}

    public String getConcatContent() {
        throw new UnsupportedOperationException("Not supported yet.");
    }

}
